package com.example.onion.hxchat.activity;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.onion.hxchat.fragment.FriendApplyNotificationFragment;
import com.example.onion.hxchat.util.PassWordHelp;
import com.example.superyi.myteam.R;
import com.hyphenate.chat.EMClient;

/**
 * @author:onion
 * @data: 2020/7/8
 */
public class FriendApplyNotificationActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private final String MAIN_FRAGMENT = "FriendApplyFragment";//fragment的标识
    private FragmentManager fragmentManager;
    private String TAG = "FriendApplyActivity";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏项目名
        setContentView(R.layout.friend_apply_activity);
        initView();
    }

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏项目名
//        setContentView(R.layout.activity_info);
//        initView();
//    }
    private void initView()
    {
        //加载布局一定要正确  重复指挥被一个活动运用 当前活动可能显示不出来
        toolbar = findViewById(R.id.info_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        // Get a support ActionBar corresponding to this toolbar
        fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.apply_framelayout, new FriendApplyNotificationFragment(), MAIN_FRAGMENT);
        fragmentTransaction.commit();
    }

    /**
     * 集成环信退出登录 EMClient.getInstance().logout(true)；
     * 顺便实现删除保存的登录的用户的账号，完成LoginActivity类说明的功能
     */
    private void logout()
    {
        //将空值的用户账户和密码保存，覆盖先前的数据实现清除账户数据，
        // 以便可以通过PassWordHelp能否获取数据判断用户登录状态
        PassWordHelp.savePassword(FriendApplyNotificationActivity.this,"","",false);
        String accountId = PassWordHelp.readPassword(FriendApplyNotificationActivity.this)[0];
        String pwd = PassWordHelp.readPassword(FriendApplyNotificationActivity.this)[1];
        Log.e("INfoActivity","保存null之后获取的accountId"+accountId);
        Log.e("INfoActivity","保存null之后获取的pwd"+pwd);
        EMClient.getInstance().logout(true);
        Intent intent = new Intent(FriendApplyNotificationActivity.this,LoginActivity.class);
        startActivity(intent);
        Toast.makeText(FriendApplyNotificationActivity.this, "退出登录", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //这里指定当前活动在上面要显示的导航栏菜单的资源xml
        getMenuInflater().inflate(R.menu.toolbar_friend_notification_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home://系统自带的返回箭头的资源id
                finish();
                Log.d("FriendApplyActivity", "返回 ");
                break;
            case R.id.friend_notification_toolbar_logout://退出登录的选项
                Log.d("FriendApplyActivity", "退出登录");
                logout();
                break;
            default:
                break;
        }
        return true;
    }




}
